/*
 * @Author: 葛文杰
 * @Date: 2021/2/2 13:52
 * @Description:
 */

const Service = require('egg').Service;
const ReportInfoProxy = require('../proxy/reportInfoProxy');
const { IS_DELETE } = require('../util/common');
const { buildTableData } = require('../util/utils');

class ReportInfoService extends Service {

    /**
     * 查询报表列表
     * @param reportName 报表名称
     * @param pageNo 页码
     * @param pageSize 分页数量
     * @return {Promise<{total: number, tableData: PermissionStatus}>}
     */
    async getReportList(reportName, pageNo, pageSize) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            const reportList = await ReportInfoProxy.getReportList(conn, reportName, pageNo, pageSize);
            const total = await ReportInfoProxy.getReportTotal(conn, reportName);
            await conn.commit(); // 提交事务
            return buildTableData(reportList, total, pageNo, pageSize);
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 获取报表信息
     * @return {Promise<PermissionStatus>}
     */
    async getReportInfo() {
        const conn = await this.app.mysql.beginTransaction();
        try {
            const reportInfo = await ReportInfoProxy.getReportInfo(conn);
            await conn.commit(); // 提交事务
            return reportInfo;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    };

    /**
     * 保存报表信息
     * @return
     */
    async insertReportInfo(reportName) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            const count = await ReportInfoProxy.countReportNumByName(conn, reportName);
            if (count >= 1) {
                // TODO 这边不写提交要紧吗
                await conn.commit(); // 提交事务
                return false;
            }
            let reportInfo = {
                reportName,
                createUser: 1,
            };
            const result = await ReportInfoProxy.insertReportInfo(conn, reportInfo);
            await conn.commit(); // 提交事务
            return result;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    };

    /**
     * 修改报表信息
     * @return {Promise<string>}
     */
    async updateReportInfo() {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let reportInfo = {
                id: 1,
                reportName: '葛文杰测试1',
            };
            const result = await ReportInfoProxy.updateReportInfo(conn, reportInfo);
            await conn.commit(); // 提交事务
            return result;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    };

    /**
     * 逻辑删除报表信息
     * @param reportInfoId 报表信息id
     * @return {Promise<void>}
     */
    async deleteReportInfoById(reportInfoId) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let reportInfo = {
                id: reportInfoId,
                isDelete: IS_DELETE,
            };
            const result = await ReportInfoProxy.updateReportInfo(conn, reportInfo);
            await conn.commit(); // 提交事务
            return result;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }
}

module.exports = ReportInfoService;
